package com.moyu.crowdsource.validator.contraint;

import com.moyu.crowdsource.conf.MyMvcConfigurer;
import com.moyu.crowdsource.service.AccountUserService;
import com.moyu.crowdsource.validator.MyConstraint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MyConstraintValidator implements ConstraintValidator<MyConstraint, Object> {

  @Autowired AccountUserService accountUserService;

  private static final Logger LOGGER = LoggerFactory.getLogger(MyConstraintValidator.class);

  @Override
  public void initialize(MyConstraint constraintAnnotation) {
     System.out.println("MyConstraintValidator init"+constraintAnnotation);
  }

  @Override
  public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {

    String name = (String) o;
    name = name.toLowerCase();
    LOGGER.info(name);
    LOGGER.info(String.valueOf(name.indexOf("wzb")));
    if (name.indexOf("wzb") != -1||name.indexOf("zb") != -1) {
      return false;
    }
    if (o == null || o.equals("") || o == "") {
      return false;
    }
    return accountUserService.chackAccountUsername((String) o);
  }
}
